package com.wenwu.controller.wenxian;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsLiterature;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.utils.JsonUtils;
import com.wenwu.common.utils.Status.AttachmentFrom;
import com.wenwu.service.CommonService;
import com.wenwu.service.LiteratureService;

/**
 * 文献资料管理（包括历史文献学术论文政策法律其他文书）
* @ClassName: LiteratureController 
* @Description: TODO 
* @author 肖崇高  xiaochonggao@zkingsoft.com 
* @date 2015年5月3日 下午11:12:17 
*
 */
@Controller
@RequestMapping(value = "/literature")
public class LiteratureController {
	Logger log = Logger.getLogger(this.getClass());
	@Autowired LiteratureService service;
	@Autowired
	private CommonService commonService;
	
	private JsonGenerator jsonGenerator;
	@RequestMapping(value = "/all")
	public @ResponseBody List<RelicsLiterature> selectAll(Model model,HttpServletRequest request){
		List<RelicsLiterature> list = null ;
		log.info("进入该方法了....");
		try {
			list = service.selectAll();
			/*for(RelicsLiterature rl:list){
				log.info("文献数据："+rl.getDicValue()+"   "+rl.getLiteratureDesc());
			}*/
		} catch (RelicsException e) {
			model.addAttribute("error",e.getMessage());
		}
		return list;
	}
	// 进入页面
	@RequestMapping(value = "/index")
	public String initPage() {
		return "literature/literature-index";
	}
	
	// 进入添加页面
	@RequestMapping(value = "/form")
	public String turnToForm(Model model,
			@RequestParam(required = false, value = "literatureId") Integer literatureId) {
		try {
			if (literatureId != null) {
				List<RelicsAttachment> attachments;
				attachments = commonService.selectAttachmentListByKey(
						literatureId, AttachmentFrom.ELICS_LITERATURE_01);
				model.addAttribute("attachments", attachments);
			}
		} catch (RelicsException e) {
			log.info(e.getMessage(),e);
		}
		return "literature/literature-form";
	}
	/**
	 * 显示要修改的信息
	 * 
	 * @param response
	 * @param userId
	 * @throws Exception
	 */
	@RequestMapping(value = "/showUpdate")
	public void showUpdate(HttpServletResponse response,
			@RequestParam(value = "literatureId") Integer literatureId){
		if (literatureId != null) {
			try{
				RelicsLiterature relicsLiterature = service.selectByKey(literatureId);
				if(relicsLiterature!=null){
					jsonGenerator = JsonUtils.getJsonGenerator(response);
					jsonGenerator.writeObject(relicsLiterature);
				}
			}catch (Exception e){
				log.info(e.getMessage(),e);
			}
		}
	}
	
	/**
	 * 删除信息
	 * 
	 * @param response
	 * @param request
	 * @param key
	 * @throws Exception
	 */
	@RequestMapping(value = "/delete")
	public void deleteInfo(HttpServletResponse response,
			HttpServletRequest request,
			@RequestParam(required = false, value = "keys") String keys)
			throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		try {
			String result = service.deleteBykeys(keys);
			jsonGenerator.writeObject(result);
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			jsonGenerator.writeObject("删除数据失败");
		}
	}
	
	/**
	 * 添加
	 */
	@RequestMapping(value = "/add")
	public void addBasic(RelicsLiterature relicsLiterature,HttpServletResponse response) throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		log.info("添加/修改数据：");
		try {
			jsonGenerator.writeObject(service.addObj(relicsLiterature));
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage(),e);
			jsonGenerator.writeObject(e.getMessage());
		}
	}
	
}
